package com.lizard.schedule.ui.view.schedulelist;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * 该下拉刷新ListView不接收横向滑动的触摸事件，因为子item需要这个事件来横向滑动
 */
public class ScheduleListView extends PullToRefreshListView {

    private final String TAG = ScheduleListView.class.getSimpleName();

    /**
     * 触摸方式：为知
     */
    public static final int TOUCH_MODE_UNKNOW = 0;

    /**
     * 触摸方式：ListView
     */
    public static final int TOUCH_MODE_LISTVIEW = 1;

    /**
     * 触摸方式：Item左右滑动
     */
    public static final int TOUCH_MODE_ITEM = 2;

    /**
     * 用作最小滑动距离的判断
     */
    private int mTouchSlop;

    private float mTouchX;

    private float mTouchY;

    /**
     * 触摸方式，事件传递给谁
     */
    private int mTouchMode = TOUCH_MODE_UNKNOW;

    /**
     * 按下时触碰到的对应listview的item
     */
    private ScheduleItemLinearLayout mTouchListItem;

    public ScheduleListView(Context context) {
        super(context);
        init(context);
    }

    public ScheduleListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ScheduleListView(Context context, Mode mode) {
        super(context, mode);
        init(context);
    }

    public ScheduleListView(Context context, Mode mode, AnimationStyle style) {
        super(context, mode, style);
        init(context);
    }

    private void init(Context context) {
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        setListViewOnTouchListener();
    }

    private void setListViewOnTouchListener(){
        getRefreshableView().setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initActionDown(event);
                        break;
                    case MotionEvent.ACTION_MOVE:

                        if(!isActionDownInited){
                            // 解决item里面的按钮 手指滑动的时候 并没有经过这里的action_down事件产生的问题
                            initActionDown(event);
                            break;
                        }

                        if (mTouchMode == TOUCH_MODE_UNKNOW) {
                            float dx = Math.abs(event.getX() - mTouchX);
                            float dy = Math.abs(event.getY() - mTouchY);

                            if (dx >= mTouchSlop || dy >= mTouchSlop) { // 大于最小滑动距离再处理
                                if (dx > dy) { // 横向滑动 则拦截事件给自身onTouchEvent
                                    mTouchMode = TOUCH_MODE_ITEM;
                                } else {
                                    mTouchMode = TOUCH_MODE_LISTVIEW;
                                    // 缩起所有item展开的选项
                                    closeAllItemOptions();
                                }
                            }
                        } else if (mTouchMode == TOUCH_MODE_ITEM) {
                            if (mTouchListItem != null) {
                                // 传递触摸事件给子view
                                mTouchListItem.onTouchEvent(event);
                            }
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        if (mTouchMode == TOUCH_MODE_ITEM) {
                            if (mTouchListItem != null) {
                                // 传递触摸事件给子view
                                mTouchListItem.onTouchEvent(event);
                            }
                        }
                        isActionDownInited = false;
                        break;
                }

                if (mTouchMode == TOUCH_MODE_ITEM) {
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    /**
     * 表示是否在手势按下时初始化过
     */
    boolean isActionDownInited = false;

    /**
     * 手势按下时初始化
     *
     * @param event
     */
    private void initActionDown(MotionEvent event){
        if(!isActionDownInited){
            isActionDownInited = true;
            mTouchX = event.getX();
            mTouchY = event.getY();
            mTouchMode = TOUCH_MODE_UNKNOW;
            mTouchListItem = null;

            int childIndex = getRefreshableView().pointToPosition((int) event.getX(), (int) event.getY()) - getRefreshableView().getFirstVisiblePosition();
            if (childIndex >= 0) {
                View child = getRefreshableView().getChildAt(childIndex);
                if (child instanceof ScheduleItemLinearLayout) {
                    mTouchListItem = (ScheduleItemLinearLayout) child;
                }
            }

            if (mTouchListItem != null) {
                // 缩起其他item展开的选项
                closeAllItemOptionsExceptThese(mTouchListItem);
            }
        }
    }

    /**
     * 缩起所有item展开的选项
     */
    public void closeAllItemOptions(){
        closeAllItemOptionsExceptThese();
    }

    /**
     * 缩起所有item展开的选项 除了指定的几个以外
     *
     * @param excepts
     */
    public void closeAllItemOptionsExceptThese(ScheduleItemLinearLayout...excepts){
        for(int i = 0; i < getRefreshableView().getChildCount(); i ++){
            View child = getRefreshableView().getChildAt(i);
            if(child instanceof ScheduleItemLinearLayout){
                ScheduleItemLinearLayout scheduleItemLinearLayout = (ScheduleItemLinearLayout)child;
                if(scheduleItemLinearLayout.isOptionsOpen()){

                    boolean isExcept = false;
                    for(ScheduleItemLinearLayout except: excepts){
                        if(except == scheduleItemLinearLayout){
                            isExcept = true;
                            break;
                        }
                    }

                    if(!isExcept){
                        scheduleItemLinearLayout.closeOptions();
                    }
                }
            }
        }
    }

}
